package com.example.demo.read;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.example.demo.read
 * @date 2020/11/14 21:27
 */
public class ReadFiles {

    public ReadFiles() {
    }

    /**
     *
     * @param file 文件
     * @param target 文件要存放的地址
     * @return
     * @throws IOException
     */
    public static boolean uploadFile(MultipartFile file, String target) throws IOException {
        String filePath = file.getOriginalFilename(); // 文件的全路径
        String[] split = filePath.split("/");
        String fileName = split[split.length-1];;// 图片文件名
        String newFilePath = "";
        for (int i =0 ;i<split.length-1;i++){
            newFilePath = newFilePath +"\\"+split[i];
        }
        newFilePath = target+"\\"+newFilePath;
        File file1 = new File(newFilePath);
        if (!file1.exists()){
            file1.mkdirs();
        }
        String allFilePath = newFilePath +"\\"+fileName;
        File file2 = new File(allFilePath);
        if (!file2.exists()){
            file2.createNewFile();
        }

        file.transferTo(file2);




        return true;

    }

}
